// import PoolDB from './pool'
// import DaxieAdaptor from './dexieAdaptor'
// import LokiAdaptor from './lokiAdaptor'
// import AbsurdSqlAdaptor from './absurdSqlAdaptor'
import CacheStorageAdaptor from './cacheStorageAdaptor'

// const db = new PoolDB({
//   adaptor: 'DaxieAdaptor'
// })

const db = new CacheStorageAdaptor()

db.init()

class CacheBox {
  async get(timestamp) {
    // console.time('get')
    const doc = await db.query({ timestamp })
    // console.timeEnd('get')

    if (doc) {
      return {
        ...doc,
        data: doc.data
      }
    }

    return doc
  }

  async update({ timestamp, data }) {
    // console.time('insert')

    if (await this.get(timestamp)) {
      return
    }

    await db.insert({ timestamp, data: JSON.stringify(data) })

    // console.timeEnd('insert')
  }

  getAll() {
    return db.queryAll()
  }
}

export default new CacheBox()
